﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;

using ScreensInterfaces;

namespace WorkFlows
{

    public sealed class GetTextActivity : NativeActivity<ScreenOutput>
    {
        [RequiredArgument]
        // Define an activity input argument of type string
        public InArgument<string> BookmarkName { get; set; }

        private string bookmarkName;

        // Define an activity input argument of type string
        public InArgument<string> Text { get; set; }

        // Define an activity input argument of type string
        public InArgument<string> Header { get; set; }

        private IScreen screen;

        protected override void Execute(NativeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            bookmarkName = context.GetValue(this.BookmarkName);
            string text = context.GetValue(this.Text);
            string header = context.GetValue(this.Header);

            ScreenInput input = new ScreenInput();
            input.Header = header;
            input.Text1 = text;
            screen = ServiceLocator.ServiceLocator.Instance.RepresentationLayerMain.ShowCaptureNumeric(input);

            screen.UserEnteredInput += OnInputReady;

            // Create a Bookmark and wait for it to be resumed.
            context.CreateBookmark(bookmarkName,
                new BookmarkCallback(OnResumeBookmark));
        }

        private void OnInputReady(object sender, UserInputReadyEventArgs e)
        {
            screen.UserEnteredInput -= OnInputReady;
            ServiceLocator.ServiceLocator.Instance.CurrentWorkFlow.ResumeBookmark(bookmarkName, e.ScreenOutput);
        }

        // NativeActivity derived activities that do asynchronous operations by calling 
        // one of the CreateBookmark overloads defined on System.Activities.NativeActivityContext 
        // must override the CanInduceIdle property and return true.
        protected override bool CanInduceIdle
        {
            get { return true; }
        }

        public void OnResumeBookmark(NativeActivityContext context, Bookmark bookmark, object obj)
        {
            // When the Bookmark is resumed, assign its value to
            // the Result argument.
            context.RemoveAllBookmarks();
            Result.Set(context, (ScreenOutput)obj);
        }
    }
}
